using System;
using System.Collections;
using System.Collections.Generic;

namespace Kugar.Core.Security.Permission
{
    public class PermissionCollection:ICollection<PermissionOperation>
    {
        private Dictionary<string ,PermissionOperation> permissionList=new Dictionary<string, PermissionOperation>();
        
        public void Add(PermissionOperation permission)
        {
            if (permission==null || string.IsNullOrEmpty(permission.ActionName.Trim()))
            {
                throw new ArgumentOutOfRangeException("permission");
            }

            lock (permissionList)
            {
                permissionList.Remove(permission.ActionName);
                permissionList.Add(permission.ActionName,permission);                
            }


            //PermissionOperation tempPermission = GetPermissionByName(permission.ActionName);

            //if (tempPermission!=null)
            //{
            //    if (permission.IsEnabled==false)
            //    {
            //        tempPermission.IsEnabled = false;
            //    }
            //}
            //else
            //{
            //    base.Add(permission);
            //}
        }

        public void Clear()
        {
            lock (permissionList)
            {
                permissionList.Clear();
            }
        }

        public bool Contains(PermissionOperation item)
        {
            return permissionList.ContainsKey(item.ActionName);
        }

        public void CopyTo(PermissionOperation[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public bool Remove(PermissionOperation item)
        {
            return permissionList.Remove(item.ActionName);
        }

        public int Count
        {
            get { return permissionList.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool CheckPermission(string actionName)
        {
            var tempPermission = GetPermissionByName(actionName);

            return tempPermission != null && tempPermission.IsEnabled;
        }

        public PermissionOperation GetPermissionByName(string permissionName)
        {

            lock (permissionList)
            {
                if (permissionList.ContainsKey(permissionName))
                {
                    return permissionList[permissionName];
                }
                else
                {
                    return null;
                }                
            }


            //PermissionOperation tempPermission = null;

            //lock (this)
            //{
            //    foreach (var p in this)
            //    {
            //        if (p.ActionName == permissionName)
            //        {
            //            tempPermission = p;
            //            break;
            //        }
            //    }
            //}

            //return tempPermission;
        }

        public IEnumerator<PermissionOperation> GetEnumerator()
        {
            return permissionList.Values.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}